package cn.com.ejiakanghu.job;

import org.linlinjava.litemall.db.dao.AgedmallDoctorInfoMapper;
import org.linlinjava.litemall.db.dao.AgedmallServiceOrderMapper;
import org.linlinjava.litemall.db.dao.AgedmallTakeOrderRecordMapper;
import org.linlinjava.litemall.db.dao.order_manage.AgedmallServiceOrderManageMapper;
import org.linlinjava.litemall.db.domain.*;
import org.linlinjava.litemall.db.util.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;

/**
 * 检查订单是否超时未服务
 * @author huangfei
 * @create 2021-12-17 16:45
 */
@Component
public class ServiceOrderTask {

    private static final Logger logger = LoggerFactory.getLogger(ServiceOrderTask.class);

    @Resource
    private AgedmallServiceOrderMapper agedmallServiceOrderMapper;

    @Resource
    private AgedmallDoctorInfoMapper agedmallDoctorInfoMapper;

    @Resource
    private AgedmallTakeOrderRecordMapper agedmallTakeOrderRecordMapper;

   // @Scheduled(cron = "0/5 * * * * ? ") // 间隔5秒执行
    public void execute(){
         //this.specifiedDoctor();
        this.timeOutDoctor();
    }


    /**
     * 超时医生接单
     */
    private void timeOutDoctor(){
      //查询状态为3并且allot不为1的数据
        AgedmallServiceOrderExample example = new AgedmallServiceOrderExample();
        example.or().andStatEqualTo(3).andStarttimeIsNull().andAllotNotEqualTo(1).andStarttimeIsNull();
        List<AgedmallServiceOrder> agedmallServiceOrderList = agedmallServiceOrderMapper.selectByExample(example);
        if(agedmallServiceOrderList.size() > 0){
            for(AgedmallServiceOrder aso : agedmallServiceOrderList){
                //根据预约编号查询状态为3的信息,抢单信息表
                AgedmallTakeOrderRecordExample takeOrderRecordExample = new AgedmallTakeOrderRecordExample();
                takeOrderRecordExample.or().andStateEqualTo(3).andSubidEqualTo(aso.getSubid());
                AgedmallTakeOrderRecord agedmallTakeOrderRecord =  agedmallTakeOrderRecordMapper.selectOneByExample(takeOrderRecordExample);
                if(agedmallTakeOrderRecord != null){
                    //判断接单是否超时
                    Date systemDate = new Date();
                    LocalDateTime ldt = systemDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
                    Duration differenceValue = Duration.between(agedmallTakeOrderRecord.getUpdateTime(),ldt);
                    Long minutesTime = differenceValue.toMinutes();
                    if(minutesTime >= 30){

                    }
                }
            }
        }

    }


    /**
     * 指定医生接单
     */
    private void specifiedDoctor(){
        AgedmallServiceOrderExample agedmallServiceOrderExample = new AgedmallServiceOrderExample();
        agedmallServiceOrderExample.or().andAllotEqualTo(1).andStarttimeIsNull();
        List<AgedmallServiceOrder> agedmallServiceOrderList =
                agedmallServiceOrderMapper.selectByExample(agedmallServiceOrderExample);
        if(agedmallServiceOrderList.size() >0){
            for(AgedmallServiceOrder aso : agedmallServiceOrderList){
                this.getServiceTimeout(aso);
            }
        }
    }

    /**
     * 检查超过30分钟后，设置为超时服务
     * @return
     */
    private Boolean getServiceTimeout(AgedmallServiceOrder serviceOrder){
        //通过医生id查询医生姓名
        AgedmallDoctorInfoExample example = new AgedmallDoctorInfoExample();
        example.or().andUserIdEqualTo(serviceOrder.getDoctorId());
       AgedmallDoctorInfo doctorInfo = agedmallDoctorInfoMapper.selectOneByExample(example);

        Date systemDate = new Date();
        LocalDateTime ldt = systemDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
        Duration differenceValue = Duration.between(serviceOrder.getAgreetime(),ldt);
        Long minutesTime = differenceValue.toMinutes();
        if(minutesTime >= 30){//超过半小时，设置超时未接单状态
            //根据医生id和预约号查询,修改该订单的状态
            AgedmallServiceOrderExample serviceOrderExample = new AgedmallServiceOrderExample();
            serviceOrderExample.createCriteria()
                    .andDoctorIdEqualTo(doctorInfo.getUserId())
                    .andSubidEqualTo(serviceOrder.getSubid());
            AgedmallServiceOrder agedmallServiceOrder = new AgedmallServiceOrder();
            agedmallServiceOrder.setStat(8);
            agedmallServiceOrderMapper.updateByExampleSelective(agedmallServiceOrder,serviceOrderExample);

            logger.info("医生姓名为"+doctorInfo.getDoctorName()+"的预约号" + serviceOrder.getSubid() + "超时未接单");
        }
        return false;
    }



}
